/*
 * Copyright © 2014 CIB Co. Ltd.
 * All right reserved.
 */
package cn.seamus.marksman.processor.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.util.Assert;

import cn.seamus.marksman.common.Constants;
import cn.seamus.marksman.common.MarksmanException;

/**
 * 
 * 
 * <p>
 * 打开指定页面
 * </p>
 * 
 * @author zhengss
 * @version 0.0.1
 * @date 2014-4-16
 */
public class CommonPageProcessor extends AbstractProcessor {
    private static Log logger = LogFactory.getLog(CommonPageProcessor.class);

    /*
     * (non-Javadoc)
     * 
     * @see
     * cn.seamus.marksman.processor.impl.AbstractProcessor#prepareNext(java.
     * util.Map)
     */
    @Override
    protected Map<String, Object> prepareNext(Map<String, Object> params) throws MarksmanException {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * cn.seamus.marksman.processor.impl.AbstractProcessor#read(java.util.Map)
     */
    @Override
    protected ProcessResult read(Map<String, Object> requestParams) throws MarksmanException {
        logger.info("Open page begin");

        Assert.notNull(requestParams);

        String pageUrl = (String) requestParams.get(Constants.MAP_KEY_NEXT_PAGE);
        String pageMethod = (String) requestParams.get(Constants.MAP_KEY_METHOD);
        if (StringUtils.isBlank(pageUrl)) {
            return null;
        }

        URL pageURL = null;

        try {
            pageURL = new URL(pageUrl);
        } catch (MalformedURLException e) {
            throw new MarksmanException("Cannot create cartPageURL: pageUrl=" + pageUrl, e);
        }

        logger.info("URL= " + pageURL.toString());
        logger.info("Connecting...");
        CloseableHttpResponse response = null;

        if (pageMethod == null || pageMethod.equalsIgnoreCase("get")) {
            response = executeGet(pageURL, null);
        } else if (pageMethod.equalsIgnoreCase("post")) {
            response = executePost(pageURL, null);
        } else {
            throw new MarksmanException("Method needed!");
        }

        ProcessResult result = new ProcessResult();
        Map<String, Object> resMap = new HashMap<String, Object>();
        result.setParams(resMap);
        try {

            String body = EntityUtils.toString(response.getEntity());

            Document doc = Jsoup.parse(body);

        } catch (IOException e) {
            throw new MarksmanException("Cannot parse  content", e);
        } finally {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        return result;
    }

}
